//
//  WDProductDynamicController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/29.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDProductDynamicController.h"
#import "WDPDHeaderCell.h"
#import "WDPDContentCell.h"
#import "WDProductDynamic.h"
#import "WDProductDetailController.h"

@interface WDProductDynamicController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSArray<WDProductDynamic *> *dataArray;

@end

@implementation WDProductDynamicController

static NSString * const headerCell = @"headerCell";

static NSString * const contentCell = @"contentCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    // 布局
    [self setUpUI];
    
    if (self.isNotify) {
        
        if ([self.product_status intValue]) {
            
            [self loadNavItemWithLeftAction:@selector(clickBackBtn) leftBtnImage:@"icon_back" leftBtnSelImage:@"icon_back" title:@"产品更新动态" rihgtAction:@selector(clickDetailBtn) rightBtnImage:@"icon_detail" rightBtnSelImage:@"icon_detail"];
        } else {
            
            // 设置导航条按钮
            [self loadNavItemWithLeftAction:@selector(clickBackBtn) leftBtnImage:@"icon_back" leftBtnSelImage:@"icon_back" title:@"产品更新动态" rihgtAction:nil rightBtnImage:nil rightBtnSelImage:nil];
            
        }
        
    } else {
        
        // 设置导航条按钮
        [self loadNavItemWithLeftAction:@selector(clickBackBtn) leftBtnImage:@"icon_back" leftBtnSelImage:@"icon_back" title:@"产品更新动态" rihgtAction:nil rightBtnImage:nil rightBtnSelImage:nil];
        
    }
    
    //发网络请求，获得数据
    [self sendRequestData];
}

- (void)setUpUI{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    tableView.backgroundColor = WDBackColor;
    
    tableView.sectionFooterHeight = 0;
    
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    
    [tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(0);
        make.bottom.equalTo(16);
    }];
    
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WDPDHeaderCell class]) bundle:nil] forCellReuseIdentifier:headerCell];
    
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WDPDContentCell class]) bundle:nil] forCellReuseIdentifier:contentCell];
}
#pragma mark - 发网络请求
- (void)sendRequestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%@", WDURLPrefix, WDURLLogin]];
    if([cookiesdata length]) {
        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
        NSHTTPCookie *cookie;
        for (cookie in cookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }
    }
    
    NSDictionary *parameters = @{
                                 @"product_id":self.productID,
                                 @"ctype":@3
                                 };
    NSString *url = [NSString stringWithFormat:@"%@%@", WDURLPrefix, WDURLProductDynamic];
    
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.dataArray = [WDProductDynamic mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //WDLog(@"%@",error);
        
    }];
}

#pragma mark - 数据源、代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else
    {
        return self.dataArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) { // 头部视图
        
        WDPDHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:headerCell];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.logo = self.parentLogo;
        
        cell.parentName = self.productName;
        
        return cell;
    }else{ // 内容cell
        
        WDPDContentCell *cell = [tableView dequeueReusableCellWithIdentifier:contentCell];
        
        [cell setDataWithModel:self.dataArray[indexPath.row] index:indexPath.row maxIndex:self.dataArray.count - 1];
        
        cell.productDynamic = self.dataArray[indexPath.row];
        
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return 125;
            
        }
            break;
            
        case 1:
        {
            WDProductDynamic *productDynamic = self.dataArray[indexPath.row];
            
            return productDynamic.cellH;
        }
            break;
            
        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 16;
}
#pragma mark - 返回按钮点击
- (void)clickBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickDetailBtn
{
    WDProductDetailController *tempVC = [[WDProductDetailController alloc] init];
    tempVC.isDynamic = YES;
    tempVC.productId = self.productID;
    tempVC.productName = self.productName;
    tempVC.view.backgroundColor = WDBackColor;
    [self.navigationController pushViewController:tempVC animated:YES];
}

@end
